package com.darren.archive.controller;

import com.darren.archive.entity.SysFunction;
import com.darren.archive.service.SysFunctionService;
import com.darren.framework.bean.ResponseBean;
import com.darren.framework.controller.BaseController;
import com.darren.framework.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "系统功能/菜单", tags = "系统功能/菜单接口", description = "系统功能/菜单接口")
@RestController
public class SysFunctionController extends BaseController {

    @Autowired
    private SysFunctionService sysFunctionService;


    /**
     * 根据系统菜单名称，获取系统功能/菜单树型结构（包含启用 和  禁用的）
     *
     * @param functionName 系统菜单名称，用于模糊查询
     * @return
     */
    @RequiresPermissions("menu:view")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "functionName", value = "系统菜单名称", paramType = "query", dataType = "String")
    })
    @ApiOperation(value = "功能/菜单树形列表", notes = "获取所有系统功能/菜单树型结构（包含启用 和  禁用的）")
    @GetMapping("/sys/function/list")
    public ResponseBean getSysFunctionList(@RequestParam(name = "functionName", required = false) String functionName) {
        functionName = StringUtils.isEmpty(functionName)?"":functionName;
        return ResponseBean.isOk(sysFunctionService.getFunctionList(functionName));
    }


    /**
     * 获取系统功能/菜单树型结构
     *
     * @return
     */
    @ApiOperation(value = "功能/菜单树", notes = "获取已启用的系统功能/菜单树型结构")
    @GetMapping("/sys/function/tree")
    public ResponseBean getSysFunctionTree() {
        return ResponseBean.isOk(sysFunctionService.getFunctionTree());
    }


    /**
     * 保存菜单信息
     *
     * @param record 菜单信息
     * @return
     */
    @RequiresPermissions("menu:edit")
    @ApiOperation(value = "保存菜单信息", notes = "保存菜单信息 SysFunction，如果 id 为空，则新增；id 不为空，则修改")
    @PostMapping(value = "/sys/function/save", consumes = "application/json;charset=UTF-8")
    public ResponseBean save(@RequestBody @ApiParam(name = "保存菜单信息", value = "传入json格式", required = true) SysFunction record) {
        return ResponseBean.isOk(sysFunctionService.save(record));
    }


    /**
     * 根据 id 删除 菜单信息
     *
     * @param id 菜单ID
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path", dataType = "Long")
    })
    @RequiresPermissions("menu:delete")
    @ApiOperation(value = "根据id 删除 菜单信息", notes = "根据id 删除 菜单信息")
    @DeleteMapping(value = "/sys/function/{id}")
    public ResponseBean deleteById(@PathVariable(name = "id") Long id) {
        sysFunctionService.deleteById(id);
        return ResponseBean.isOk();
    }


    /**
     * 根据id 查询 菜单信息
     *
     * @param id 菜单Id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单id", required = true, paramType = "path", dataType = "Long")
    })
    @RequiresPermissions("menu:view")
    @ApiOperation(value = "根据id 查询 菜单信息", notes = "根据id 查询 菜单信息")
    @GetMapping(value = "/sys/function/{id}")
    public ResponseBean getById(@PathVariable(name = "id") Long id) {
        return ResponseBean.isOk(sysFunctionService.findById(id));
    }


    /**
     * 保存是否启用状态
     *
     * @param record 菜单信息
     * @return
     */
    @RequiresPermissions("menu:edit")
    @ApiOperation(value = "保存是否启用状态", notes = "保存是否启用状态")
    @PostMapping(value = "/sys/function/enable/save", consumes = "application/json;charset=UTF-8")
    public ResponseBean saveEnable(@RequestBody @ApiParam(name = "菜单信息", value = "传入json格式", required = true) SysFunction record) {
        return ResponseBean.isOk(sysFunctionService.saveEnable(record));
    }


}
